<template>
	<om-addpage style="padding-top: 35px;" :menuName="menuName" @formSubmit="formSubmit" :busCode="busCode">
		<om-autoform ref="autoform" :initConfigData="mainConfigData" :initValue="initValue"></om-autoform>
	</om-addpage>
</template>

<script>
	import {
		getUrl,
		confirmModal,
		errorModal,
		ptFloat,
		minusF
	} from '@/static/js/common.js';
	import {
		buildFormData,
		formValidation,
		tranferToEditors,
		changeEditor,
		setSubmitSubValue,
		configSelectData,
		hasValue,
		hasEditor,
		getEditorTitle,
		changeConfigAttr,
		formShowLoading,
		getFormFields,
		formModelLoad,
		getWorkCacheKey
	} from '@/static/js/formUtil.js';
	import urlUtil from '@/static/js/urlUtil.js';
	import { mapState } from 'pinia';
	import {useLoginUserStore} from '@/stores/login-user.js';

	export default {
		data() {
			return buildFormData({
				maintenanceInstCacheKey:"aft-bx-repairReportForm-maintenanceInstId-cache",
				repairReportUserCache:'aft-bx-repairReportForm-repairReportUser-cache',
				customerCache:'aft-bx-repairReportForm-customer-cache',
				businessOwnerCache:'aft-bx-repairReportForm-businessOwner-cache',
				customerContactCache:'aft-bx-repairReportForm-customerContact-cache',
				addressCacheKey:'aft-bx-repairReportForm-address-cache',
				maintenanceUserCache:'aft-bx-repairReportForm-maintenanceUser-cache',
			})
		},
		computed: {
			...mapState(useLoginUserStore,['userName', 'userId','salaryInstId','salaryInstName'])
		},
		provide() {
			return {
				formPage: this
			}
		},
		methods: {
			onselectExpectFinishDate(value){
				let _that = this
				let formdata = _that.$refs.autoform.formdata
				if(_that.subEditorObj['title1']){
					let dataList = _that.subEditorObj['title1'].workList
					for(let index in dataList){
						dataList[index].expectFinishDate = value
					}
					formdata.dataStr = JSON.stringify(dataList)
					if(_that.subEditorObj['title1']){
						_that.subEditorObj['title1'].workList = dataList
					}
					uni.setStorageSync('oldWorkList', JSON.stringify(dataList))
					_that.om.toast('子表数据已更新')
				}
			},
			onSubChange(editor){
				let _that = this.$refs.autoform
				if(editor.workList.length) {
					let oldWorkListStr = uni.getStorageSync("oldWorkList");
					let oldWorkListArr = JSON.parse(oldWorkListStr)
					if(oldWorkListArr.length == editor.workList.length){
						for(let i=0; i<editor.workList.length; i++){
							if(editor.workList[i].expectFinishDate != oldWorkListArr[i].expectFinishDate){
								_that.formdata.expectFinishDate = ""
							}
						}
					}
					else if(oldWorkListArr.length < editor.workList.length){
						for(let i=0; i<editor.workList.length;i++){
							if(_that.formdata.expectFinishDate != editor.workList[i].expectFinishDate){
								_that.formdata.expectFinishDate = ""
							}
						}
					}
					else{
						
					}
					uni.setStorageSync('oldWorkList', JSON.stringify(editor.workList))
				}
			},
			getWorkParam(){
				let _that = this
				let title = _that.$refs.autoform.getEditorTitle("title","repairReportTime")
				return {
					title:title,
					repairReportTime:_that.$refs.autoform.formdata.repairReportTime,
					expectFinishDate:_that.$refs.autoform.formdata.expectFinishDate,
				}
			},
			clearCustomerContact(){
				let _that = this
				_that.$refs.autoform.clear("title","customerContactId")
				_that.$refs.autoform.clear("title","customerContactPhone")
			},
			clearCustomer(){
				let _that = this
				_that.$refs.autoform.clear("title","customerId")
				_that.$refs.autoform.clear("title","customerName")
				_that.$refs.autoform.clear("title","customerContactId")
				_that.$refs.autoform.clear("title","customerContactPhone")
			},
			clearBusinessOwner(){
				let _that = this
				_that.$refs.autoform.clear("title","businessOwnerId")
				_that.$refs.autoform.clear("title","businessInstId")
			},
			clearMaintenanceUser(){
				let _that = this
				_that.$refs.autoform.clear("title","maintenanceUserId")
				_that.$refs.autoform.clear("title","maintenanceInstId")
			},
			clearRepairReportUser(){
				let _that = this
				_that.$refs.autoform.clear("title","repairReportUserId")
				_that.$refs.autoform.clear("title","repairReportInstId")
				
			},
			getInstData(userId){
				let _that = this
				return _that.om.request(
					'orgInstPosUserOfficial/findInstByUserIdOrPostId',
					{
						userId: userId
					}
				)
			},
			async findInstByUserIdOrPostId1(id){
				let _that = this
				return await _that.getInstData(id)
			},
			verifyCustomerContact(){
				let _that = this
				if(_that.$refs.autoform.formdata.customerId){
					return true
				}else{
					let title1 = _that.$refs.autoform.getEditorTitle("title")
					let title = _that.$refs.autoform.getEditorTitle("title","customerId")
					errorModal(_that,'提示信息',title1+'中的'+title+'需要优先编辑',()=>{})
					return false
				}
			},
			getCustomerData(id){
				let _that = this
				_that.$refs.autoform.changeEditor("title","customerContactId",{
					link:'../../public/pageSearch/pageSearch',
					"linkParam":{
						other:JSON.stringify({
							customerShowId: id
						}),
						url:'customerShow/getCustomerData',
						namefield:'userName',
						notefield:'phone',
						initload:true,
						searchfield:'q',
						cachename:_that.customerContactCache
					}
				})
			},
			findInstByUserIdOrPostId(id,name){
				let _that = this
				_that.om.request(
					'orgInstPosUserOfficial/findInstByUserIdOrPostId',
					{
						userId: id
					}
				).then(res=>{
					if(name == 'repairReportUserId'){
						_that.$refs.autoform.changeEditor("title",'repairReportInstId', 'data', configSelectData(res, 'id', 'instName'))
						_that.$refs.autoform.initSelectVal("title",'repairReportInstId')
					}else if(name == 'maintenanceUserId'){
						_that.$refs.autoform.changeEditor("title",'maintenanceInstId', 'data', configSelectData(res, 'id', 'instName'))
						_that.$refs.autoform.initSelectVal("title",'maintenanceInstId')
					}else{
						_that.$refs.autoform.changeEditor("title",'businessInstId', 'data', configSelectData(res, 'id', 'instName'))
						_that.$refs.autoform.initSelectVal("title",'businessInstId')
					}
				})
			},
			initLoad(options) {
				let _that = this;
				var fileId = options.fileId
				let ajaxArr = [
					urlUtil.zposUrl(options.code), //制表岗位
					{
						url:'orgInstPosUserOfficial/findInstByUserIdOrPostId',
						param:{
							userId: _that.userId
						}
					},
					{
						url:'dictSource/loadCombo',
						param:{
							sourceId:'289',
							categoryId:'8820'
						}
					}
				]
				if(fileId){
					ajaxArr.push({
						url:'repairReportForm/getValue',
						param:{
							fileId:fileId
						}
					})
				}
				formModelLoad(ajaxArr, function(res, mainData) {
					changeEditor('topinfo', 'zposId', 'data', configSelectData(res[0], 'id', 'posName'), mainData);
					changeEditor("title",'repairReportUserId',{
						defaultName:_that.userName,
						defaultValue:_that.userId
					},null,mainData)
					changeEditor("title",'repairReportInstId',{
						defaultName:_that.salaryInstName,
						defaultValue:_that.salaryInstId
					},null,mainData)
					changeEditor("topinfo",'zinstId',{
						defaultName:_that.salaryInstName,
						defaultValue:_that.salaryInstId
					},null,mainData)
					changeEditor("title",'repairReportInstId', 'data', configSelectData(res[1], 'id', 'instName'), mainData);
					changeEditor("title",'businessInstId', 'data', configSelectData(res[1], 'id', 'instName'), mainData);
					changeEditor("title",'maintenanceInstId', 'data', configSelectData([], 'id', 'instName'), mainData);
					changeEditor("title","repairReportUserId",{
						link:'../../public/pageSearch/pageSearch',
						"linkParam":{
							other:JSON.stringify({
								firstUserId:_that.userId
							}),
							url:'temployee/getStaffCombogrid',
							namefield:'userPosInstName',
							initload:true,
							searchfield:'q',
							cachename:_that.repairReportUserCache
						}
					},null,mainData)
					changeEditor("title","businessOwnerId",{
						link:'../../public/pageSearch/pageSearch',
						"linkParam":{
							other:JSON.stringify({
								firstUserId:_that.userId
							}),
							url:'temployee/getStaffCombogrid',
							namefield:'userPosInstName',
							initload:true,
							searchfield:'q',
							cachename:_that.businessOwnerCache
						}
					},null,mainData)
					changeEditor("title","customerId",{
						link:'../../public/pageSearch/pageSearch',
						"linkParam":{
							other:JSON.stringify({
								firstId: ''
							}),
							url:'customerShow/getCustomerPageTwo',
							namefield:'customerCode',
							extrafield:'customerId',
							notefield:'customerName',
							initload:true,
							searchfield:'q',
							cachename:_that.customerCache
						}
					},null,mainData)
					changeEditor("title","maintenanceUserId",{
						link:'../../public/pageSearch/pageSearch',
						"linkParam":{
							other:JSON.stringify({
								firstUserId:''
							}),
							url:'temployee/getStaffCombogrid',
							namefield:'userPosInstName',
							initload:true,
							searchfield:'q',
							cachename:_that.maintenanceUserCache
						}
					},null,mainData)
					changeEditor("title",'askTypeId', 'data', configSelectData(res[2], 'id', 'text'), mainData);
					changeEditor("title", 'locationId','type','areabox',mainData)
					changeEditor("title", 'locationId', {
						link:'../../public/pageSearch/pageSearch',
						'linkParam':{
							other: JSON.stringify({
								classifyCode: '37056'
							}),
							searchfield: 'q',
							namefield: 'dicname',
							url: urlUtil.addressUrl().url,
							fullName:true,
							cachename: _that.addressCacheKey,
							placeholder: '请输入地址',
						}
					},null,mainData)
					changeEditor("title","maintenanceInstId",{
						link: "../../public/search/search",
						"linkParam": {
							other: JSON.stringify({
								containBoard: true
							}),
							method: "post",
							namefield: 'instName',
							searchfield: "q",
							initload: true,
							url: 'tinstitution/findNotBoard1',
							cachename: _that.maintenanceInstCacheKey,
							placeholder: "请输入部门名称"
						}
					},null,mainData)
					if(_that.subEditorObj["title1"]){
						let attr = {
							getWorkParam:'getWorkParam'
						}
						let configData = _that.subConfigData['title1']
						if(hasEditor("title1","customerProductName",configData)){
							let title = getEditorTitle("title1","customerProductName",configData)
							attr.titleTemplate = title+':<%= customerProductName %>'
						}
						changeEditor("title1","dataStr",attr,mainData)
					}
					
						if(fileId){
							let fieldArr = getFormFields(mainData)
							fieldArr.push('id','customerCode')
							uni.setStorageSync('oldWorkList', JSON.stringify(res[3].value.dataList))
							for(let index in res[3].value){
								if(fieldArr.includes(index)){
									if(index == 'customerCode'){
										_that.$refs.autoform.$set(_that.$refs.autoform.formdata,'customerShowId',res[3].value[index])
										_that.$set(_that.formdata,'customerShowId',res[3].value[index])
									}
									_that.$refs.autoform.$set(_that.$refs.autoform.formdata,index,res[3].value[index])
									_that.$set(_that.formdata,index,res[3].value[index])
								}
							}
							if(_that.subEditorObj['title1']){
								let dataList = res[3].value.dataList
								_that.subEditorObj['title1'].workList = dataList
								_that.$refs.autoform.$set(_that.$refs.autoform.formdata,'dataStr',JSON.stringify(dataList))
								_that.$set(_that.formdata,'dataStr',JSON.stringify(dataList))
							}
							changeEditor("title","customerContactId",{
								link:'../../public/pageSearch/pageSearch',
								"linkParam":{
									other:JSON.stringify({
										customerShowId: _that.$refs.autoform.formdata.customerId
									}),
									url:'customerShow/getCustomerData',
									namefield:'userName',
									notefield:'phone',
									initload:true,
									searchfield:'q',
									cachename:_that.customerContactCache
								}
							},null,mainData)
						}
					_that.$nextTick(function(){
						if(!_that.$refs.autoform.formdata.zposId){
							_that.$refs.autoform.initSelectVal("topinfo","zposId")
						}else{
							_that.$refs.autoform.initSelectIndex("topinfo","zposId")
						}
						_that.$refs.autoform.initSelectIndex("title","askTypeId")
						if(_that.edit){
							_that.findInstByUserIdOrPostId1(_that.formdata.businessOwnerId).then(res=>{
								_that.$refs.autoform.changeEditor("title","businessInstId","data",configSelectData(res,'id','instName'))
								_that.$refs.autoform.initSelectIndex("title","businessInstId")
							})
							_that.findInstByUserIdOrPostId1(_that.formdata.repairReportUserId).then(res=>{
								_that.$refs.autoform.changeEditor("title","repairReportInstId","data",configSelectData(res,'id','instName'))
								_that.$refs.autoform.initSelectIndex("title","repairReportInstId")
							})
							_that.findInstByUserIdOrPostId1(_that.formdata.maintenanceUserId).then(res=>{
								_that.$refs.autoform.changeEditor("title","maintenanceInstId","data",configSelectData(res,'id','instName'))
								_that.$refs.autoform.initSelectIndex("title","maintenanceInstId")
							})
						}
					})
				}, options)
			},
			formSubmit() {
				let _that = this
				if(_that.subEditorObj["title1"]){
					let dataList = JSON.parse(_that.formdata.dataStr)
					 for(let index in dataList){
						 if(!dataList[index].fileList){
							 delete dataList[index].fileList
						 }
					 }
					 _that.formdata.dataStr = JSON.stringify(dataList)
				}
				if(!_that.edit){
					_that.om.formSave("repairReportForm/doAdd",_that.formdata,_that)
				}else{
					_that.om.formSave("repairReportForm/doUpdate",_that.formdata,_that)
				}
			}
		},
		onUnload(){
		//#ifdef APP-PLUS
			let _that = this;
			if (_that.$refs.autoform) {
				for (var fieldId in _that.subEditorObj) {
					uni.removeStorage({
						key: getWorkCacheKey(fieldId)
					});
				}
			}
		//#endif
		},
		onLoad(options) {
			this.initValue.userinfoId = this.userId
			this.initLoad(options)
			let a = [];
			uni.setStorageSync('oldWorkList', JSON.stringify(a))
		},
		onShow() {
			let _that = this;
			if (_that.$refs.autoform) {
				for (var fieldId in _that.subEditorObj) {
					var editor = _that.subEditorObj[fieldId];
					uni.getStorage({
						key: getWorkCacheKey(fieldId),
						success: function(res) {
							editor.workList = res.data;
							_that.onSubChange(editor)
							uni.removeStorage({
								key: getWorkCacheKey(fieldId)
							});
						}
					});
				}
			}
			uni.getStorage({
				key:_that.repairReportUserCache,
				success:(res)=>{
					if(res.data){
						let d = res.data
						let formdata = _that.$refs.autoform.formdata
						formdata.repairReportUserId = d.id
						formdata.repairReportUserName =d.username
						_that.findInstByUserIdOrPostId(d.id,'repairReportUserId')
						uni.removeStorage({
							key:_that.repairReportUserCache
						})
					}
				}
			})
			uni.getStorage({
				key:_that.businessOwnerCache,
				success:(res)=>{
					if(res.data){
						let d = res.data
						let formdata = _that.$refs.autoform.formdata
						formdata.businessOwnerId = d.id
						formdata.businessOwnerName =d.username
						_that.findInstByUserIdOrPostId(d.id,'businessOwnerId')
						uni.removeStorage({
							key:_that.businessOwnerCache
						})
					}
				}
			})
			uni.getStorage({
				key:_that.customerCache,
				success:(res)=>{
					if(res.data){
						let d = res.data
						let formdata = _that.$refs.autoform.formdata
						formdata.customerId = d.id
						formdata.customerShowId =  d.customerId
						formdata.customerCode = d.customerCode
						formdata.customerName = d.customerName
						_that.$refs.autoform.clear("title","customerContactId")
						_that.$refs.autoform.clear("title","customerContactPhone")
						_that.getCustomerData(d.id)
						uni.removeStorage({
							key:_that.customerCache
						})
					}
				}
			})
			uni.getStorage({
				key:_that.customerContactCache,
				success:(res)=>{
					if(res.data){
						let d = res.data
						let formdata = _that.$refs.autoform.formdata
						formdata.customerContactId = d.id
						formdata.customerContactName = d.userName
						formdata.customerContactPhone = d.phone
						uni.removeStorage({
							key:_that.customerContactCache
						})
					}
				}
			})
			uni.getStorage({
				key: _that.addressCacheKey,
				success: function(res) {
					if (res.data) {
						let d = res.data;
						var formdata = _that.$refs.autoform.formdata;
						_that.$refs.autoform.$refs.areapop.close()
						formdata.locationId = d.id
						formdata.locationName = d.fullName
						uni.removeStorage({
							key: _that.addressCacheKey
						});
					}
				}
			});
			uni.getStorage({
				key:_that.maintenanceInstCacheKey,
				success:(res)=>{
					if(res.data){
						let d = res.data
						let formdata = _that.$refs.autoform.formdata
						formdata.maintenanceInstId = d.id
						formdata.maintenanceInstName = d.instName
						uni.removeStorage({
							key:_that.maintenanceInstCacheKey
						})
					}
				}
			});
			uni.getStorage({
				key:_that.maintenanceUserCache,
				success:(res)=>{
					if(res.data){
						let d = res.data
						let formdata = _that.$refs.autoform.formdata
						formdata.maintenanceUserId = d.id
						formdata.maintenanceUserName =d.username
						_that.findInstByUserIdOrPostId(d.id,'maintenanceUserId')
						uni.removeStorage({
							key:_that.maintenanceUserCache
						})
					}
				}
			})
		}
	}
</script>

<style>
</style>
       